<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click.native="showDialog = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table
              border=""
              :data="rolelist"
              :highlight-current-row="true"
              current-row-key="id"
              stripe
            >
              <el-table-column sortable label="序号" width="120" type="index" />
              <el-table-column
                sortable
                label="角色名称"
                width="240"
                prop="name"
              />
              <el-table-column sortable label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button
                    size="small"
                    type="success"
                    @click="handleAssignFn(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click.native="handleEditFn(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="handleDelFn(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page.currentIndex"
                :page-sizes="page.pagesizes"
                :page-size="page.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              label-width="120px"
              style="margin-top:50px"
              :model="companyInfo"
            >
              <el-form-item label="公司名称" prop="name">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址" prop="companyAddress">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱" prop="mailbox">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注" prop="remarks">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <MyDialog
      :show-dialog.sync="showDialog"
      :form-data="formData"
      @reload="getRoleFn"
    />
    <AssignPerm
      :show-perm-dialog.sync="assign.showPermDialog"
      :role-id="assign.id"
    />
  </div>
</template>

<script>
import {
  getRoleAPI,
  delRoleAPI,
  getCompanyInfoAPI,
  getRoleInfoAPI
} from '@/api/settings.js'
import MyDialog from './components/my-dialog.vue'
import AssignPerm from './components/assign-permission.vue'

export default {
  components: { MyDialog, AssignPerm },
  data() {
    return {
      rolelist: [], // 角色列表
      page: {
        currentIndex: 1, // 当前页码
        total: 0, // 中条数
        pagesize: 4, // 默认页容量
        pagesizes: [2, 4] // 页容量选项
      }, // 分页数据
      companyInfo: {}, // 公司信息
      showDialog: false, // 弹出层
      formData: {
        name: '',
        description: ''
      }, // 表单数据
      assign: {
        showPermDialog: false,
        id: '0'
      }
    }
  },
  computed: {
    companyId() {
      return this.$store.getters.companyId
    }
  },
  watch: {
    // 分页数据有任何变化都重新获取列表
    page: {
      deep: true,
      handler() {
        this.getRoleFn()
      }
    },
    // 弹层关闭时清空表单
    showDialog(val) {
      if (!val) this.formData = { name: '', description: '' }
    }
  },
  created() {
    this.getRoleFn()
    this.getCompanyInfoFn()
  },
  methods: {
    // 获取公司信息
    async getCompanyInfoFn() {
      const res = await getCompanyInfoAPI(this.companyId)
      // console.log(res)
      this.companyInfo = res
    },
    // 获取列表
    async getRoleFn() {
      const { pagesize, currentIndex } = this.page
      const res = await getRoleAPI({ page: currentIndex, pagesize })
      console.log(res)
      this.rolelist = res.rows // 列表数据
      this.page.total = res.total // 总条数
      if (this.page.pagesizes.indexOf(res.total) === -1) {
        this.page.pagesizes.push(res.total) // 把总条数加入页容量可选项
      }
    },
    // 页容量改变
    handleSizeChange(val) {
      this.page.pagesize = val
    },
    // 页码改变
    handleCurrentChange(val) {
      this.page.currentIndex = val
    },
    // 删除
    handleDelFn(id) {
      // console.log(id)
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message.success('删除成功!')
          delRoleAPI(id)
          this.page.currentIndex = 1 // 重置页码并刷新数据
        })
        .catch(() => this.$message.info('取消删除'))
    },
    // 编辑
    async handleEditFn(id) {
      const res = await getRoleInfoAPI(id)
      // console.log(res)
      this.formData = res // 数据回显
      this.showDialog = true // 打开弹层
    },
    // 分配权限
    handleAssignFn(id) {
      this.assign = {
        showPermDialog: true,
        id
      }
    }
  }
}
</script>
